package network;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;

import presenter.Presenter;

import signals.*;


public class ChatNI {
 
	private Presenter p;
	private int portUDP;
	private ServerSocket socket;
	
	public ChatNI(Presenter presenter, int pUDP,int pTCP) {
		this.p = presenter;
		this.portUDP = pUDP;
		(new UDPListener(this,portUDP)).start();
		try {
			this.socket = new ServerSocket(pTCP);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public void receiveSignal (Signal s, InetAddress addrD){
		if (s instanceof Hello) {		
			p.processHello((Hello) s,addrD);}
		else if(s instanceof HelloReply) {
			p.processHelloReply((HelloReply) s,addrD); }
		else if(s instanceof AcceptFile) {
			p.processAcceptFile((AcceptFile) s,addrD);}
		else if(s instanceof AskLogin) {
			p.processAskLogin((AskLogin) s,addrD);}
		else if(s instanceof GoodBye) {
			p.processGoodBye((GoodBye )s,addrD);}
		else if(s instanceof PropFile) {
			p.processPropFile((PropFile) s,addrD);}
		else if(s instanceof SendText) {
			p.processSendText((SendText) s,addrD);}
		else {
			System.out.println("Error decoding the signal, exiting ..");
			System.exit(-1);
		}
		
	}
	
	public void sendSignalUDP(Signal s, int portD, InetAddress addrD) {
		UDPSender uS = new UDPSender(addrD,this,portUDP);
		uS.send(s);
	}


	public void recvFileTCP(String path) {
		(new TCPListener(this,path,socket)).start();
	}


	public void sendFileTCP(String fileName, InetAddress addrD , int portTCP) {
		(new TCPSender(this,fileName,addrD,portTCP)).start();
	}


	public void dlComplete(InetAddress iAddress) {
		this.p.processDLComplete(iAddress);
		
	}
	
	
}
